<template>
  <div class="add-topic">
    <a-modal
      class="loading"
      :visible="visible"
      width="20%"
      :title="title"
      @cancel="handleClose"
      @ok="handleOk"
    >
      <a-form class="form" :model="addStudentForm" name="basic" ref="formRef" :rules="rules">
        <a-form-item label="考生姓名" name="ksxm">
          <a-input v-model:value="addStudentForm.ksxm" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="考生编号" name="ksbh">
          <a-input v-model:value="addStudentForm.ksbh" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="准考证号" name="zkzh">
          <a-input v-model:value="addStudentForm.zkzh" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="考试地点" name="bmdmc">
          <a-input v-model:value="addStudentForm.bmdmc" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="考试科目" name="bkzymc">
          <a-input v-model:value="addStudentForm.bkzymc" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="考试代码" name="bkzydm">
          <a-input v-model:value="addStudentForm.bkzydm" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="报考院系所名称" name="bkyxsmc">
          <a-input v-model:value="addStudentForm.bkyxsmc" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="报考院系所码" name="bkyxsm">
          <a-input v-model:value="addStudentForm.bkyxsm" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="报考年份" name="bknf">
          <a-input v-model:value="addStudentForm.bknf" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="报考点省市" name="bkdss">
          <a-input v-model:value="addStudentForm.bkdss" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="报考地点代码" name="bkddm">
          <a-input v-model:value="addStudentForm.bkddm" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="条码状态" name="tmzt">
          <a-select
            v-model:value="addStudentForm.tmzt"
            show-search
            allowClear
            :options="[
              { label: '未打印', value: 'wdy' },
              { label: '已打印', value: 'ydy' },
            ]"
            placeholder="请选择"
          >
            <template #suffixIcon>
              <caret-down-outlined class="ant-select-suffix" />
            </template>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted, computed, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { saveOrUpdateStudent } from '/@/api/markingStatistics/studentRegistrationInfo';
  const props = defineProps({
    visible: {
      type: Boolean,
    },
    addStudentForm: {
      type: Object,
      default: () => {},
    },
  });

  const rules = {
    ksxm: [{ required: true, message: '考生姓名不能为空', trigger: 'blur' }],
    ksbh: [{ required: true, message: '考生编号不能为空', trigger: 'blur' }],
    zkzh: [{ required: true, message: '准考证号不能为空', trigger: 'blur' }],
    bmdmc: [{ required: true, message: '考试地点不能为空', trigger: 'blur' }],
    bkzymc: [{ required: true, message: '考试科目不能为空', trigger: 'blur' }],
    bkzydm: [{ required: true, message: '考试代码不能为空', trigger: 'blur' }],
    tmzt: [{ required: true, message: '条码状态不能为空', trigger: 'blur' }],
    bkddm: [{ required: true, message: '报考地点代码不能为空', trigger: 'blur' }],
    bkdss: [{ required: true, message: '报考点省市不能为空', trigger: 'blur' }],
    bknf: [{ required: true, message: '报考年份不能为空', trigger: 'blur' }],
    bkyxsm: [{ required: true, message: '报考院系所码不能为空', trigger: 'blur' }],
    bkyxsmc: [{ required: true, message: '报考院系所名称不能为空', trigger: 'blur' }],
  };

  const emits = defineEmits(['cancel', 'refresh']);
  const title = computed(() => (props.addStudentForm.id ? '编辑考生信息' : '新增考生信息'));

  const formRef = ref();
  const handleClose = async () => {
    emits('cancel');
    await formRef.value.resetFields();
  };
  const handleOk = async () => {
    try {
      await formRef.value.validate();
      const postData = JSON.parse(JSON.stringify(props.addStudentForm));

      saveOrUpdateStudent(postData).then((res) => {
        if (res.code == 200) {
          message.success(`${title.value}成功`);
          emits('refresh');
          handleClose();
        } else {
          message.error(res.message);
        }
      });
    } catch (error) {
      console.log(error);
    }
  };
</script>
<style lang="less" scoped>
  .form {
    width: 100%;
    padding: 16px;
  }
</style>
